import Vue from 'vue'
import Vuex from 'vuex'
import $ from 'jquery'

Vue.use(Vuex)

export default new Vuex.Store({
	state: {
		tabList: [],
		tabsLeft: 0,
		choseLiftData:[]  //角色管理选择的电梯数据
	},
	//mainView页面初始化
	mutations: {
		TABLISTCHANGE(state) {
			setTimeout(()=>{
				if (localStorage.tags) {
					if (JSON.parse(localStorage.tags).list.length > 0) {
						this.state.tabList = JSON.parse(localStorage.tags).list
					} else {
						this.state.tabList = [{
							label: '数据统计',
							isColor: 1,
							pathIndex: "/MainView/OldStatistics"
						}]
						localStorage.menuActive = JSON.stringify({
							active: "/MainView/OldStatistics"
						})
					}
				} else {
					this.state.tabList = []
				}
				console.log(this.state.tabList)
				if (localStorage.tabsLeft) {
					this.state.tabsLeft = JSON.parse(localStorage.tabsLeft).left
				}
				if (localStorage.menuActive) {
					let active = JSON.parse(localStorage.menuActive).active
					Vue.router.push(active)
				}
			},10)
		},
		//新增tag
		ADDTAB(state, obj) {
			let newLabel = obj.newLabel
			let index = obj.index
			console.log(index)
			if (this.state.tabList.length != 0) {
				this.state.tabList.map((item) => {
					item.isColor = 0
				})
			}
			if (this.state.tabList.findIndex((item) => {
					return item.label == newLabel
				}) < 0) {
				this.state.tabList.push({
					label: newLabel,
					isColor: 1,
					pathIndex: index
				})
				console.log(this.state.tabList)
			} else {
				this.state.tabList.map((item) => {
					if (item.label == newLabel) {
						item.isColor = 1
					}
				})
			}
			let list = this.state.tabList
			console.log(list)
			localStorage.tags = JSON.stringify({
				list: list
			})
			localStorage.menuActive = JSON.stringify({
				active: index
			})
			setTimeout(() => {
				let length = this.state.tabList.length
				let currengIndex = ''
				this.state.tabList.forEach((item, index) => {
					if (item.isColor == 1) {
						currengIndex = index
					}
				})
				let left = $('.tagSitem').eq(currengIndex).position().left - 800
				if (left <= 0) {
					left = 0
				}
				this.state.tabsLeft = -left
				localStorage.tabsLeft = JSON.stringify({
					left: this.state.tabsLeft
				})
			}, 100)
		},
		//跳转tags
		GOTAGROUTER(state, nextRouter) {
			console.log(nextRouter)
			let newItem = ''
			this.state.tabList.map((item, index) => {
				if (item.isColor == 1) {
					 this.state.tabList.splice(index,1)
				}
			})
// 			this.state.tabList = this.state.tabList.filter((item)=>{
// 				 return typeof item != 'number' 
// 			})
			if (nextRouter.name) {
				this.state.tabList.map((item) => {
					if (item.label == nextRouter.name) {
						item.isColor = 1
					}
				})
				Vue.router.push({
					path: nextRouter.router
				})
				localStorage.tags = JSON.stringify({
					list: this.state.tabList
				})
				localStorage.menuActive = JSON.stringify({
					active: nextRouter.router
				})
				setTimeout(() => {
					let length = this.state.tabList.length
					let currengIndex = ''
					this.state.tabList.forEach((item, index) => {
						if (item.isColor == 1) {
							currengIndex = index
						}
					})
					let left = $('.tagSitem').eq(currengIndex).position().left - 800
					if (left <= 0) {
						left = 0
					}
					this.state.tabsLeft = -left
					localStorage.tabsLeft = JSON.stringify({
						left: this.state.tabsLeft
					})
				}, 10)
			}
		},
	},
	actions: {

	}
})
